package com.zzy.controller;

import com.zzy.model.AjaxMsg;
import com.zzy.service.RedisService;
import com.zzy.util.UtilNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping(value = "/redis")
public class RedisController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("/test")
    @ResponseBody
    public AjaxMsg test(){
        return AjaxMsg.returnAjaxMsg(redisService.test());
    }


    @RequestMapping("/ping")
    @ResponseBody
    public AjaxMsg isConnect(){
        return AjaxMsg.returnAjaxMsg(redisService.isConnect());
    }

    /**获取所有的key*/
    @RequestMapping("/keys")
    @ResponseBody
    public AjaxMsg getKeys(){
        return AjaxMsg.returnAjaxMsg(redisService.getKeys());
    }

    /**1.1、根据key 取 String 值*/
    @RequestMapping("/string/{key}")
    @ResponseBody
    public AjaxMsg getValueString(@PathVariable(value = "key") String key){
        return AjaxMsg.returnAjaxMsg(redisService.getValueString(key));
    }


    /**1.2、根据key 写入 String 值*/
    @RequestMapping(value = "/string",method = RequestMethod.POST)
    @ResponseBody
    public AjaxMsg setValueString(HttpServletRequest request){
        String key = request.getParameter("key");
        String val = request.getParameter("val");
        if(StringUtils.hasText(key) && StringUtils.hasText(val)){
            return AjaxMsg.returnAjaxMsg(redisService.setValueString(key,val));
        }
        return AjaxMsg.returnAjaxMsg(redisService.setValueString(key,val));
    }

    /**1.3、根据key 写入 String 值 带过期 时间*/
    @RequestMapping(value = "/string/exprie",method = RequestMethod.POST)
    @ResponseBody
    public AjaxMsg setValueStringExprie(HttpServletRequest request){
        String key = request.getParameter("key");
        String val = request.getParameter("val");
        String time = request.getParameter("time");
        if(StringUtils.hasText(key) && StringUtils.hasText(val) && StringUtils.hasText(time) && UtilNumber.isNumber1(time)){
            return AjaxMsg.returnAjaxMsg(redisService.setValueString(key,val,Integer.valueOf(time)));
        }
        return AjaxMsg.returnAjaxMsg(redisService.setValueString(key,val));
    }





    /**2.1、根据key 取 List 值*/
    @RequestMapping("/list/{key}")
    @ResponseBody
    public AjaxMsg getValueList(@PathVariable(value = "key") String key){
        return AjaxMsg.returnAjaxMsg(redisService.getValueList(key));
    }

    /**2、2 根据key 取 List 值*/
    @RequestMapping("/list/{key}/{start}/{end}")
    @ResponseBody
    public AjaxMsg getValueList(@PathVariable(value = "key") String key,@PathVariable(value = "start") Integer start,@PathVariable(value = "end") Integer end){
        return AjaxMsg.returnAjaxMsg(redisService.getValueList(key));
    }

    /**2.3、根据key 写入 list */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public AjaxMsg setValueList(HttpServletRequest request){
        List list = new ArrayList();
        String key = request.getParameter("key");
        return AjaxMsg.returnAjaxMsg(redisService.setValueList(key,list));
    }





    /**3.1、根据key 取 Hash 值 */
    @RequestMapping("/hash/{key}")
    @ResponseBody
    public AjaxMsg getValueHash(@PathVariable(value = "key") String key){
        return AjaxMsg.returnAjaxMsg(redisService.getValueHash(key));
    }
    /**3.2、根据key 写入 Hash 值 */
    @RequestMapping(value = "/hash",method = RequestMethod.POST)
    @ResponseBody
    public AjaxMsg setValueHash(HttpServletRequest request){
        String key = request.getParameter("key");
        String mapkey = request.getParameter("mapkey");
        String mapVal = request.getParameter("mapVal");
        if(StringUtils.hasText(key) && StringUtils.hasText(mapkey) && StringUtils.hasText(mapVal) ){
            return AjaxMsg.returnAjaxMsg(redisService.setValueHash(key,mapkey,mapVal));
        }
        return AjaxMsg.returnAjaxMsg();
    }
    /**3.3、根据key 写入 Hash 值 */
    @RequestMapping(value = "/hash/map",method = RequestMethod.POST)
    @ResponseBody
    public AjaxMsg setValueHashmap(HttpServletRequest request){
        Map map = new HashMap();
        String key = request.getParameter("key");

        if(StringUtils.hasText(key)  ){
            return AjaxMsg.returnAjaxMsg(redisService.setValueHash(key, map ));
        }
        return AjaxMsg.returnAjaxMsg();
    }





    /**4.1、根据key 取 Set 值 */
    @RequestMapping("/set/{key}")
    @ResponseBody
    public AjaxMsg getValueSet(@PathVariable(value = "key") String key){
        return AjaxMsg.returnAjaxMsg(redisService.getValueSet(key));
    }
    /**4.2、根据key 写入 Set 值 */
    @RequestMapping(value = "/set",method = RequestMethod.POST)
    @ResponseBody
    public AjaxMsg setValueSet(HttpServletRequest request){
        String key = request.getParameter("key");
        String[] obj = new String[] {};
        if(StringUtils.hasText(key)  ){
            return AjaxMsg.returnAjaxMsg(redisService.setValueSet(key, obj ));
        }
        return AjaxMsg.returnAjaxMsg();
    }
    /**4.3  移除 set中某个元素 */
    @RequestMapping(value = "/set/remove/cloum",method = RequestMethod.POST)
    @ResponseBody
    public AjaxMsg removeSetCloum(HttpServletRequest request){
        Object[] obj  = new Object[]{};
        String key = request.getParameter("key");
        if(StringUtils.hasText(key)  ){
            return AjaxMsg.returnAjaxMsg(redisService.removeSetCloum(key,obj));
        }
        return AjaxMsg.returnAjaxMsg();
    }



    /**5.1、根据key 取 zset 值 成员不能重复 */
    @RequestMapping("/zset/{key}")
    @ResponseBody
    public AjaxMsg getValueZSet(@PathVariable(value = "key") String key){
        return AjaxMsg.returnAjaxMsg(redisService.getValueZSet(key));
    }

    /**5.2、根据key 取 zset 值 成员不能重复 */
    @RequestMapping("/zset/{key}/{start}/{end}")
    @ResponseBody
    public AjaxMsg getValueZSet(@PathVariable(value = "key") String key,@PathVariable(value = "start") Integer start,@PathVariable(value = "end") Integer end){
        return AjaxMsg.returnAjaxMsg(redisService.getValueZSet(key,start, end));
    }
    /**5.3、根据key 写入 zset 值 成员不能重复 */
    @RequestMapping(value = "/zset",method = RequestMethod.POST)
    @ResponseBody
    public AjaxMsg setValueZSet(HttpServletRequest request){
        Set set = new HashSet();
        String key = request.getParameter("key");

        if(StringUtils.hasText(key)  ){
            return AjaxMsg.returnAjaxMsg(redisService.setValueZSet(key, set ));
        }
        return AjaxMsg.returnAjaxMsg();
    }

    /**5.4、根据key 写入 zset 值 成员不能重复 排序 */
    @RequestMapping(value = "/zset/order",method = RequestMethod.POST)
    @ResponseBody
    public AjaxMsg setValueZSetOrder(HttpServletRequest request){
        String key = request.getParameter("key");
        String val = request.getParameter("val");
        String order = request.getParameter("order");
        if(StringUtils.hasText(key) && StringUtils.hasText(val) && StringUtils.hasText(order) && UtilNumber.isNumber1(order) ){
            return AjaxMsg.returnAjaxMsg(redisService.setValueZSet(key,val,Integer.valueOf(order)));
        }
        return AjaxMsg.returnAjaxMsg();
    }



    /**6、删除*/
    @RequestMapping(value = "/delete/key",method = RequestMethod.DELETE)
    @ResponseBody
    public AjaxMsg delete(HttpServletRequest request){
        String key = request.getParameter("key");
        if(StringUtils.hasText(key)  ){
            return AjaxMsg.returnAjaxMsg(redisService.delete(key));
        }
        return AjaxMsg.returnAjaxMsg();
    }



}
